import { LangStore } from "~/stores/modules/lang";
import { GlobalStore } from "@/stores"

export default defineNuxtRouteMiddleware(async (to, from) => {
  const { setReqHeader } = LangStore();
  if (process.server) {
    let reqHeader = useRequestHeaders();
    setReqHeader(reqHeader);
    // 设置referer
    const nuxtApp = useNuxtApp();
    let host: string = "";
    let firstRefer = nuxtApp.ssrContext?.event.node.req.headers.referer
      ? nuxtApp.ssrContext.event.node.req.headers.referer.split("/")[0]
      : "";

    let http = "";
    if (firstRefer === "http:" || firstRefer === "https:")
      http = firstRefer + "//";
    host = nuxtApp.ssrContext?.event.node.req.headers.host
      ? http + nuxtApp.ssrContext?.event.node.req.headers.host
      : "";
    const useGlobalStore = GlobalStore()
    useGlobalStore.setReferer(host)
  }
});
